﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.UpgradesUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Events.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import org.aswing.geom.*;
    import Display.*;
    import org.aswing.ext.*;
    import Display.aswingui.*;

    public class UpgradesCrewCell extends CrewCell {

        protected var m_gridList:GridList;
        protected var m_index:int;
        protected var m_crewObject:Object;
        protected var m_color:int;
        protected var m_portraitPanel:JPanel;
        protected var m_namePanel:JPanel;
        protected var m_statusPanel:JPanel;
        protected var m_empty:Boolean = false;
        protected var m_filled:Boolean = false;

        public function UpgradesCrewCell(_arg1:CrewCellFactory, _arg2:int){
            this.m_color = _arg2;
            super(_arg1, layout);
        }
        override public function setCellValue(_arg1):void{
            if (_arg1 == "empty"){
                this.m_empty = true;
            } else {
                this.m_crewObject = _arg1;
                if (this.m_crewObject.count > 0){
                    this.m_filled = true;
                };
            };
            this.buildCell();
            ASwingHelper.prepare(this);
            dispatchEvent(new Event(UIEvent.PREPARE, true));
        }
        override public function buildCell():void{
            var _local2:Sprite;
            var _local3:JPanel;
            var _local4:JPanel;
            this.removeAll();
            var _local1:JPanel = new JPanel(new BorderLayout());
            if ((this.m_color % 2) == 0){
                _local2 = new Sprite();
                _local2.graphics.beginFill(EmbeddedArt.lightBlueTextColor);
                _local2.graphics.drawRect(0, 0, UpgradesCrewScrollingList.ROW_WIDTH, UpgradesCrewScrollingList.ROW_HEIGHT);
                _local2.graphics.endFill();
                ASwingHelper.setBackground(_local1, _local2);
            };
            _local1.setPreferredSize(new IntDimension(UpgradesCrewScrollingList.ROW_WIDTH, UpgradesCrewScrollingList.ROW_HEIGHT));
            _local1.setMinimumSize(new IntDimension(UpgradesCrewScrollingList.ROW_WIDTH, UpgradesCrewScrollingList.ROW_HEIGHT));
            _local1.setMaximumSize(new IntDimension(UpgradesCrewScrollingList.ROW_WIDTH, UpgradesCrewScrollingList.ROW_HEIGHT));
            if (this.m_empty == false){
                _local3 = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
                _local4 = ASwingHelper.makeFlowJPanel(FlowLayout.RIGHT);
                _local3.appendAll(this.makeImagePanel(), this.makeNamePanel());
                _local4.append(this.makeCheckPanel());
                _local1.append(_local3, BorderLayout.WEST);
                _local1.append(_local4, BorderLayout.EAST);
            };
            this.append(_local1);
        }
        override public function setGridListCellStatus(_arg1:GridList, _arg2:Boolean, _arg3:int):void{
            if (_arg1){
                this.m_gridList = _arg1;
                this.m_index = _arg3;
                _arg1.setTileWidth(UpgradesCrewScrollingList.ROW_WIDTH);
                _arg1.setTileHeight(UpgradesCrewScrollingList.ROW_HEIGHT);
            };
        }
        override protected function makeImagePanel():JPanel{
            var image:* = null;
            var ap:* = null;
            var jp:* = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            image = (new (UpgradesCommonAssets.assetDict["blankPortrait"])() as DisplayObject);
            if (this.m_filled == true){
                LoadingManager.load(this.m_crewObject.url, function (_arg1:Event):void{
                    image = _arg1.target.content;
                    ap.setAsset(image);
                });
            };
            ap = new AssetPane(image);
            jp.append(ap);
            return (jp);
        }
        protected function makeNamePanel():JPanel{
            var _local3:String;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            var _local2:String = TextFieldUtil.formatSmallCapsString((this.m_crewObject.name as String));
            if (this.m_filled == true){
                _local3 = this.m_crewObject.footerText;
            } else {
                _local3 = ZLoc.t("Dialogs", "HireFriendsToFill");
            };
            var _local4:JLabel = ASwingHelper.makeLabel(_local2, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 14, EmbeddedArt.blueTextColor);
            var _local5:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            _local5.append(_local4);
            _local1.append(_local5);
            var _local6:JLabel = ASwingHelper.makeLabel(_local3, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 14, (this.m_filled) ? EmbeddedArt.brownTextColor : EmbeddedArt.blueTextColor);
            var _local7:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            _local7.append(_local6);
            _local1.append(_local7);
            return (_local1);
        }
        protected function makeCheckPanel():JPanel{
            var _local2:DisplayObject;
            var _local3:Sprite;
            var _local4:JPanel;
            var _local5:JPanel;
            var _local6:AssetIcon;
            var _local7:CustomButton;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            if (this.m_filled){
                _local1.setPreferredSize(new IntDimension(60, 53));
                _local1.setMinimumSize(new IntDimension(60, 53));
                _local1.setMaximumSize(new IntDimension(60, 53));
                _local2 = (new (UpgradesCommonAssets.assetDict["checkMark"])() as DisplayObject);
                _local3 = new Sprite();
                _local3.addChild(_local2);
                _local1.addChild(_local3);
                _local3.x = ((50 / 2) - (_local2.width / 2));
                _local3.y = ((50 / 2) - (_local2.height / 2));
            } else {
                _local1.setPreferredHeight(53);
                _local1.setMinimumHeight(53);
                _local1.setMaximumHeight(53);
                _local4 = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
                _local5 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
                _local6 = new AssetIcon((new EmbeddedArt.icon_cash() as DisplayObject));
                _local7 = new CustomButton(TextFieldUtil.formatSmallCapsString(ZLoc.t("Dialogs", "FillForX", {cash:this.m_crewObject.cost})), _local6, "CashButtonUI");
                _local7.addEventListener(MouseEvent.CLICK, this.onCashBuy, false, 0, true);
                _local7.setPreferredHeight(30);
                _local7.setMinimumHeight(30);
                _local7.setMaximumHeight(30);
                _local5.appendAll(_local7, ASwingHelper.horizontalStrut(5));
                _local4.append(_local5);
                _local1.append(_local4);
            };
            ASwingHelper.prepare(this);
            return (_local1);
        }
        protected function onCashBuy(_arg1:Event):void{
            var _local2:Boolean;
            var _local3:Player;
            if ((((this.m_filled == false)) && ((this.m_empty == false)))){
                _local2 = this.m_crewObject.buyCallback(this.m_crewObject.cost);
                if (_local2){
                    _local3 = Global.player.findFriendById("-1");
                    this.m_crewObject.url = _local3.snUser.picture;
                    this.m_crewObject.footerText = _local3.firstName;
                    this.m_crewObject.count = 1;
                    this.setCellValue(this.m_crewObject);
                    this.dispatchEvent(new Event(MunicipalUpgradesDialogView.UPDATE_POSITIONS, true));
                } else {
                    UI.displayPopup(new GetCoinsDialog(ZLoc.t("Dialogs", "ImpulseMarketCash"), "HireFriendsButtonLabel", GenericDialogView.TYPE_GETCASH, null, true));
                };
            };
        }
        public function rebuildCellAfterFillAll():void{
            var _local1:Player;
            if ((((this.m_filled == false)) && ((this.m_empty == false)))){
                _local1 = Global.player.findFriendById("-1");
                this.m_crewObject.url = _local1.snUser.picture;
                this.m_crewObject.footerText = _local1.firstName;
                this.m_crewObject.count = 1;
                this.setCellValue(this.m_crewObject);
            };
        }

    }
}//package Display.UpgradesUI 
